package lib.bone.system.module.iojob.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lib.bone.system.module.iojob.constant.JobModeConst;
import lib.bone.system.module.iojob.constant.JobStatusConst;
import lib.bone.system.module.iojob.constant.JobTypeConst;
import lib.bone.system.module.iojob.dao.IOJobDAO;
import lib.bone.system.module.iojob.domain.IOJob;
import lib.bone.system.module.iojob.service.IOJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@Service
public class IOJobServiceImpl implements IOJobService {
    @Value("${file-upload-service.path}")
    private String fileParentPath;

    @Autowired
    IOJobDAO dao;

    @Override
    public IOJob createImportJob() {
//        String createrId = StpUtil.getLoginIdAsString();
        IOJob ioJob = new IOJob();
//        ioJob.setCreaterId(createrId);
        ioJob.setJobMode(JobModeConst.INSERT);
        ioJob.setJobType(JobTypeConst.IMPORT);
        ioJob.setJobStatus(JobStatusConst.IN_PROGRESS);
        return dao.save(ioJob);
    }

    @Override
    public IOJob createExportJob(String fileName) {
        String createrId = StpUtil.getLoginIdAsString();
        IOJob ioJob = new IOJob();
        ioJob.setCreaterId(createrId);
        ioJob.setFileName(fileName);
        ioJob.setJobMode(JobModeConst.UPSERT);
        ioJob.setJobType(JobTypeConst.EXPORT);
        ioJob.setJobStatus(JobStatusConst.IN_PROGRESS);

        return dao.save(ioJob);
    }

    @Override
    public PageResultDTO<IOJob> getAll(Pageable pageable, Example<IOJob> example) {
        Page<IOJob> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }else {
            page = dao.findAll(example,pageable);
        }
        List<IOJob> list = page.getContent();
        PageResultDTO<IOJob> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<IOJob> deleteById(String id) {
        dao.deleteById(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @Override
    public Optional<IOJob> getById(String id) {
        return dao.findById(id);
    }

    @Override
    public IOJob saveOne(IOJob ioJob) {
        if (Validator.isNotEmpty(ioJob.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(ioJob);
    }

    @Override
    public IOJob updateOne(IOJob ioJob) {
        if (Validator.isEmpty(ioJob.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(ioJob);
    }
}
